﻿// ********************************************
// ClassName  ：  {filename}
// Author        ：  
// Company     ： 智测心控
// Version        ：V1.0
// CreateTime ： {date}
// Description ：  
// ********************************************

using DAL.DB;
using DAL.Enum;
using Newtonsoft.Json;
using System;
using System.Data.Entity;
using System.Linq;

namespace WcfService.ReviewService
{
    /// <summary>
    /// 回顾服务
    /// </summary>
    public class ReviewService : IReviewService
    {
        /// <summary>
        /// 事件回顾信息查询
        /// </summary>
        /// <param name="patId">病人ID</param>
        /// <param name="time">时间</param>
        /// <param name="level">级别</param>
        /// <param name="limit">每页多少条</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>事件回顾信息</returns>
        public string EventReview(Guid patId, DateTime? time, int level, int limit, int pageIndex)
        {
            using (var db = new YFrameContext())
            {
                var eventReviewInfos = db.EventReview.Where(o => o.PatientId.Equals(patId)).ToList();

                if (time != null)
                {
                    eventReviewInfos = eventReviewInfos.Where(u => u.CreateTime.Value.ToShortDateString().Equals(time.Value.ToShortDateString())).ToList();
                }

                if (level != 4)
                {
                    eventReviewInfos = eventReviewInfos.Where(o => (int)o.EventLevel == level).ToList();
                }

                var reviewResult = eventReviewInfos.OrderByDescending(o => o.CreateTime);

                var json = new
                {
                    DataCount = reviewResult.Count(),
                    Data = JsonConvert.SerializeObject(reviewResult.Skip((pageIndex - 1) * limit).Take(limit).ToList()),
                };

                return JsonConvert.SerializeObject(json);
            }
        }

        /// <summary>
        /// NIBP回顾页面
        /// </summary>
        /// <param name="patId">病人ID</param>
        /// <param name="time">日期</param>
        /// <param name="nsFrom">收缩压开始</param>
        /// <param name="nsTo">收缩压结束</param>
        /// <param name="ndFrom">舒张压开始</param>
        /// <param name="ndTo">舒张压结束</param>
        /// <param name="nmFrom">平均压开始</param>
        /// <param name="nmTo">平均压结束</param>
        /// <param name="limit">每页数量</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>NIBP回顾信息</returns>
        public string NibpReview(Guid patId, DateTime? time, 
            string nsFrom, string nsTo, string ndFrom, string ndTo, string nmFrom, string nmTo, 
            int limit, int pageIndex)
        {
            using (var db = new YFrameContext())
            {
                var nibpInfos = db.NibpRecord.Where(o => o.PatientId.Equals(patId)).ToList();

                // 日期过滤
                if (time != null)
                {
                    nibpInfos = nibpInfos.Where(u => u.CreateTime.Value.ToShortDateString().Equals(time.Value.ToShortDateString())).ToList();
                }

                // 收缩压过滤
                if (!string.IsNullOrWhiteSpace(nsFrom))
                {
                    nibpInfos = nibpInfos.Where(u => u.Ns >= Convert.ToDouble(nsFrom)).ToList();
                }

                if (!string.IsNullOrWhiteSpace(nsTo))
                {
                    nibpInfos = nibpInfos.Where(u => u.Ns <= Convert.ToDouble(nsTo)).ToList();
                }

                // 舒张压过滤
                if (!string.IsNullOrWhiteSpace(ndFrom))
                {
                    nibpInfos = nibpInfos.Where(u => u.Nd >= Convert.ToDouble(ndFrom)).ToList();
                }

                if (!string.IsNullOrWhiteSpace(ndTo))
                {
                    nibpInfos = nibpInfos.Where(u => u.Nd <= Convert.ToDouble(ndTo)).ToList();
                }

                // 平均压过滤
                if (!string.IsNullOrWhiteSpace(nmFrom))
                {
                    nibpInfos = nibpInfos.Where(u => u.Nm >= Convert.ToDouble(nmFrom)).ToList();
                }

                if (!string.IsNullOrWhiteSpace(nmTo))
                {
                    nibpInfos = nibpInfos.Where(u => u.Nm <= Convert.ToDouble(nmTo)).ToList();
                }

                var nibpResult = nibpInfos.OrderByDescending(o => o.CreateTime);

                var json = new
                {
                    DataCount = nibpResult.Count(),
                    Data = JsonConvert.SerializeObject(nibpResult.Skip((pageIndex - 1) * limit).Take(limit).ToList()),
                };

                return JsonConvert.SerializeObject(json);
            }
        }

        /// <summary>
        /// 获取监护记录
        /// </summary>
        /// <param name="time">归档日期</param>
        /// <param name="patientName">患者姓名</param>
        /// <param name="patientType">患者类型</param>
        /// <param name="limit">每页数量</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>监护记录Json字符串</returns>
        public string MonitoringRecordsReview(DateTime? time, string patientName, int patientType, int limit, int pageIndex)
        {
            using (var db = new YFrameContext())
            {
                var mRecords = db.MonitoringRecords.Include(x => x.PatientInfo).ToList();

                // 日期过滤
                if (time != null)
                {
                    mRecords = mRecords.Where(u => u.ArchiveTime.ToShortDateString().Equals(time.Value.ToShortDateString())).ToList();
                }

                // 患者姓名过滤
                if (!string.IsNullOrWhiteSpace(patientName))
                {
                    mRecords = mRecords.Where(u => u.PatientInfo.PatientName.Contains(patientName)).ToList();
                }

                // 患者类型过滤
                if (patientType != (int)EnumPatientTyp.All)
                {
                    mRecords = mRecords.Where(u => (int)u.PatientInfo.PatientType == patientType).ToList();
                }

                var reiviewResult = mRecords.OrderByDescending(o => o.ArchiveTime);

                var json = new
                {
                    DataCount = reiviewResult.Count(),
                    Data = JsonConvert.SerializeObject(reiviewResult.Skip((pageIndex - 1) * limit).Take(limit).ToList()),
                };

                return JsonConvert.SerializeObject(json);
            }
        }

        /// <summary>
        /// 趋势回顾根据时间查询波形数据
        /// </summary>
        /// <param name="patId">病人Id</param>
        /// <param name="statTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>趋势回顾波形数据</returns>
        public string TrendMapReview(Guid patId, DateTime? statTime, DateTime? endTime)
        {
            var json = new
            {
                DataCount = 0,
                Data = "",
            };

            return JsonConvert.SerializeObject(json);
        }

        /// <summary>
        /// 趋势回顾根据时间查询表格数据
        /// </summary>
        /// <param name="patId">病人Id</param>
        /// <param name="statTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>趋势回顾表格数据</returns>
        public string TrendGridReview(Guid patId, DateTime? statTime, DateTime? endTime)
        {
            using (var db = new YFrameContext())
            {
                var eventReviewInfos = db.EventReview.Where(o => o.PatientId.Equals(patId)).ToList();

                if (statTime != null)
                {
                    eventReviewInfos = eventReviewInfos.Where(u => u.CreateTime.Value >= statTime).ToList();
                }

                if (endTime != null)
                {
                    eventReviewInfos = eventReviewInfos.Where(o => o.CreateTime < endTime).ToList();
                }

                var reviewResult = eventReviewInfos.OrderByDescending(o => o.CreateTime);

                var json = new
                {
                    DataCount = reviewResult.Count(),
                    Data = JsonConvert.SerializeObject(reviewResult.ToList()),
                };

                return JsonConvert.SerializeObject(json);
            }
        }
    }
}
